﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RFIDStockManagement.IDAL;
using System.Data.Common;

namespace RFIDStockManagement.DAL
{
    public class MakeItemRfidAccess : IMakeItemRfid
    {

        public long CreateNewItemRfid(long itemId, long unitId, long count)
        {
            long result = 0;
            DbHelper helper = new DbHelper();
            string sql = string.Format(@"DECLARE @Rfid bigint
SET @Rfid=0
SELECT @Rfid=FRfid FROM rfid_Item_Rfid WHERE FItemID={1} AND FUnitID={2}
IF @Rfid=0
BEGIN
SET @Rfid=@Rfid+{0}
INSERT INTO rfid_Item_Rfid(FItemID,FUnitID,FRfid)VALUES({1},{2},@Rfid)
END
ELSE
BEGIN
SET @Rfid=@Rfid+{0}
UPDATE rfid_Item_Rfid SET FRfid=@Rfid WHERE FItemID={1} AND FUnitID={2}
END
SELECT @Rfid", count, itemId, unitId);
            DbCommand comm = helper.GetSqlStringCommand(sql);
            object scalar = helper.ExecuteScalar(comm);
            if (scalar != null)
            {
                result = Convert.ToInt64(scalar);
            }

            return result;
        }
    }
}
